package com.wn.inventory.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.ActivitiStartEntity;
import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.Hr;
import com.wn.entity.ScrapApplication;
import com.wn.inventory.feign.ActivitiFeignClient;
import com.wn.inventory.mapper.ScrapApplicationMapper;
import com.wn.inventory.service.ScrapApplicationService;
import com.wn.inventory.vo.ScrapApplicationVo;
import com.wn.utils.CurrentTime;
import com.wn.utils.PageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/22 14:41
 * @ description：
 * @ modified By：
 * @ version:
 */
@Service
public class ScrapApplicationImpl implements ScrapApplicationService {
    @Autowired
    ScrapApplicationMapper scrapApplicationMapper;
    @Autowired
    ActivitiFeignClient activitiFeignClient;
    /**
     * @param scrapApplication
     * @return void
     * @ author nst
     * @ date 2022/10/22 14:06
     * @ Description 增加报废申请信息
     */
    @Override
    public void insert(ScrapApplication scrapApplication, HttpServletRequest request) {
        scrapApplication.setApplicant(new Hr(Integer.parseInt(request.getHeader("userId"))));
        scrapApplication.setScrapTime(CurrentTime.getCurrentTime());
        String identifier = "SA" + CurrentTime.getCurrentTimeString();
        scrapApplication.setIdentifier(identifier);
        ResponseEntity<String> entity = activitiFeignClient.startInstance(new ActivitiStartEntity("in_warehouss_application_id", "user1", request.getHeader("userWorkNumber")));
//        activitiFeignClient.completeTask(new ActivitiCompleteEntity("in_warehouss_application_id",entity.getData(),"user1",));
        scrapApplication.setActivitiInstanceId(entity.getData());
        scrapApplicationMapper.insert(scrapApplication);
    }

    /**
     * @param scrapApplicationVo
     * @return PageBean<ScrapApplication>
     * @ author nst
     * @ date 2022/10/22 14:07
     * @ Description 多条件分页查询报废申请信息
     */
    @Override
    public PageBean<ScrapApplication> getByCondition(ScrapApplicationVo scrapApplicationVo) {
        Page<ScrapApplicationVo> page = PageHelper.startPage(scrapApplicationVo.getCurrPage(), scrapApplicationVo.getPageSize());
        List<ScrapApplication> list = scrapApplicationMapper.getByCondition(scrapApplicationVo.getScrapApplication());
        return PageBeanUtil.getPageBean(list, page, scrapApplicationVo.getCurrPage(), scrapApplicationVo.getPageSize());
    }
}
